package com.suray.wcs.web.system.controller;

import com.suray.commin.annotation.Log;
import com.suray.commin.core.controller.BaseController;
import com.suray.commin.core.page.TableDataInfo;
import com.suray.commin.enmus.BusinessType;
import com.suray.commin.core.controller.ResponseData;
import com.suray.wcs.service.system.bo.AssignConfigBO;
import com.suray.wcs.service.system.bo.PageResultBO;
import com.suray.wcs.service.system.entity.AssignConfigDB;
import com.suray.wcs.service.system.service.impl.AssignConfigServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/assignConfig")
public class AssignConfigController extends BaseController {

    @Autowired
    private AssignConfigServiceImpl assignConfigServiceImpl;

    /**
     * 新增配置
     *
     * @throws Exception
     */
    @PostMapping(value = "/addAssignConfig")
    @PreAuthorize("@ss.hasPermi('assignConfig:add')")
    @Log(title = "分车配置", method = "新增分车配置", businessType = BusinessType.INSERT)
    @ResponseBody
    @ApiOperation("新增分车配置")
    public ResponseData addAssignConfig(@RequestBody AssignConfigDB assignConfigDB) {
        if (assignConfigDB.getFloor() == null) {
            return ResponseData.error("层不存在");
        }
        if (assignConfigDB.getRgvLimit() == null) {
            return ResponseData.error("限制小车数量不存在");
        }
        //验重
        AssignConfigDB assignConfigDBTemp = assignConfigServiceImpl.getAssignConfigByFloorAndZoneId(assignConfigDB.getFloor(), assignConfigDB.getZoneId());
        if (assignConfigDBTemp != null) {
            return ResponseData.error("该层已存在");
        }
        boolean flag = assignConfigServiceImpl.insertAssignConfig(assignConfigDB);
        if (flag) {
            return ResponseData.success("插入成功");
        } else {
            return ResponseData.error("插入失败");
        }
    }


    /**
     * 删除配置
     *
     * @throws Exception
     */
    @DeleteMapping(value = "/deleteAssignConfig/{ids}")
    @PreAuthorize("@ss.hasPermi('assignConfig:remove')")
    @Log(title = "分车配置", method = "删除分车配置", businessType = BusinessType.DELETE)
    @ResponseBody
    @ApiOperation("删除分车配置")
    public ResponseData deleteAssignConfig(@PathVariable List<Integer> ids) {

        boolean flag = assignConfigServiceImpl.deleteAssignConfig(ids);
        if (flag) {
            return ResponseData.success("删除成功");
        } else {
            return ResponseData.error("删除失败");
        }
    }


    /**
     * 更新配置
     *
     * @throws Exception
     */
    @PostMapping(value = "/updateAssignConfig")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('assignConfig:edit')")
    @Log(title = "分车配置", method = "修改分车配置", businessType = BusinessType.UPDATE)
    @ApiOperation("修改分车配置")
    public ResponseData updateAssignConfig(@RequestBody AssignConfigDB assignConfigDB) {


        if (assignConfigDB.getFloor() == null) {
            return ResponseData.error("层不存在");
        }
        if (assignConfigDB.getRgvLimit() == null) {
            return ResponseData.error("限制小车数量不存在");
        }
        boolean flag = assignConfigServiceImpl.updateAssignConfig(assignConfigDB);
        if (flag) {
            return ResponseData.success("修改成功");
        } else {
            return ResponseData.error("修改失败");
        }
    }

    /**
     * 获取配置
     *
     * @throws Exception
     */
    @GetMapping(value = "/getAssignConfigList")
    @ResponseBody
    @PreAuthorize("@ss.hasPermi('assignConfig:query')")
    @ApiOperation("查询分车配置")
    public TableDataInfo getAssignConfigList(AssignConfigDB assignConfigDB) {
        startPage();
        List<AssignConfigDB> assignConfigDBList = assignConfigServiceImpl.getAssignConfigList(assignConfigDB);
        return getDataTable(assignConfigDBList);
    }

    /**
     * 获取配置
     *
     * @throws Exception
     */
    @GetMapping(value = "/getAssignConfigInfo/{id}")
    @ResponseBody
    @ApiOperation("查询分车配置")
    public ResponseData getAssignConfigInfo(@PathVariable Integer id) {
        AssignConfigBO assignConfigInfo = assignConfigServiceImpl.getAssignConfigInfo(id);
        return ResponseData.success("深位规则查询完成", assignConfigInfo);
    }

}
